#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
const ll N=200005;
ll n,q,c,ans,a[N],fa[N],fa2[N],sz[N],vs[N];
int ans3[1024][50005];
pll E[N];
vector<int> ans2[128];
vector<ll> T[N];
bool chk(ll u,ll s){
	return (s>>a[u])&1;
}
void bfs(ll u,ll s,ll id){
	queue<ll> q;
	if(chk(u,s)){
		q.push(u);
		vs[u]=id;
	}
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		ans++;
		for(ll y:T[x]){
			if(vs[y]!=id&&chk(y,s)){
				q.push(y);
				vs[y]=id;
			}
		}
	}
}
void solve1(){
	rep(i,1,q){
		ll s,u;
		cin>>s>>u;
		if(s==(1ll<<c)-1){
			cout<<n<<'\n';
		}
		else{
			ans=0;
			bfs(u,s,i);
			cout<<max(ans,1ll)<<'\n';
		}
	}
}
ll find(ll u){
	return u==fa2[u]?u:fa2[u]=find(fa2[u]);
}
void solve2(){
	rep(i,1,(1ll<<c)-1){
		rep(j,1,n){
			fa2[j]=j;
			sz[j]=1;
		}
		rep(j,1,n-1){
			ll u=E[j].fi,v=E[j].se;
			if(chk(u,i)&&chk(v,i)){
				ll fu=find(u),fv=find(v);
				sz[fv]+=sz[fu];
				fa2[fu]=fv;
			}
		}
		rep(j,1,n){
			if(c<=7){
				ans2[i].pb(sz[find(j)]);
			}
			else if(c<=10){
				ans3[i][j]=sz[find(j)];
			}
		}
	}
	rep(i,1,q){
		ll s,u;
		cin>>s>>u;
		if(!s){
			cout<<1<<'\n';
		}
		else if(s==(1ll<<c)-1){
			cout<<n<<'\n';
		}
		else if(c<=7){
			cout<<max(1,ans2[s][u-1])<<'\n';
		}
		else if(c<=10){
			cout<<max(1,ans3[s][u])<<'\n';
		}
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0);
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	cin>>n>>q>>c;
	rep(i,1,n){
		cin>>a[i];
	}
	rep(i,2,n){
		ll u;
		cin>>u;
		E[i-1]={u,i};
		T[u].pb(i);
		T[i].pb(u);
	}
	if((n<=200000&&c<=5)||(n<=50000&&c<=10)){
		solve2();
	}
	else{
		solve1();
	}
	return 0;
}
